import {query,serial,Audits, driverAudit,MFTAut} from '../services/AuditUser'
import { parse } from 'qs'
import { isEmptyObject,ModalPrompting,mergeJSON } from '../utils'

export default {

  namespace: 'au',

  state: {
    list:[],
    detail:[],
    individualVo:[],
    ApprovedsTagBon:false,
    DetailTags:false,
    Editstags:false,
    IndexListBon:true,
    autStatus:'',
    pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null,
    },
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/AuditUser') {
          dispatch({
            type: 'query',
            payload: location.query,
          })
          dispatch({
            type: 'initialization',
          })
        }
      })
    },
  },
  effects: {
    *query ({ payload }, { call, put }) {
        let page = {};  
        let autStatus ='';
        if(payload.autStatus && payload.autStatus !='item_0'){
          
          autStatus=payload.autStatus
        }else{
          autStatus=''
        }
        if(isEmptyObject(payload.page)){
          page = {total:0,current:1,pageSize:10,keyword:true,autStatus:autStatus}
        }else{
          page = {current:payload.page,pageSize:payload.pageSize,keyword:false,autStatus:autStatus}
        }
        const data = yield call(query, parse(page))
        if (data) {
          const detail = yield call(serial, data.data.rows[0].id)
          console.log("detail===333",detail)
          if(detail){
            yield put({type:'showDetail'})
            yield put({
              type: 'querySuccess',
              payload: {
                list: data.data.rows,
                pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
                autStatus:autStatus,
                detail: detail.data.rows,
              }
            })
          }
        }
    },
    *detail({payload},{call,put}){
        const data = yield call(serial, payload.id)
        console.log("data===",data)
        yield put({type:'showDetail'})
        if(data){
            yield put({
                type: 'detailsit',
                payload: {
                    detail:data.data.rows.vehicleVo,
                    individualVo:data.data.rows.individualVo,
                }
            })
        }
    },
    *driverAut({payload},{call,put}){
      const data = yield call(driverAudit, payload)
      if(data.data.status == 200){
        if(payload.aut ==1){
          if(data.data.rows.individualStatus ==0 && data.data.rows.vehicleStatus ==0){
            yield put({type:'showApproved'})
          }else{
            const individualStatus = data.data.rows.individualStatus == -1? '个人信息不全、':''
            const vehicleStatus = data.data.rows.vehicleStatus == -1? '车辆信息不全':''
            const title = {
              tit:'认证失败',
              center:'用户的'+individualStatus+vehicleStatus +'，认证不能通过。'
            }
            ModalPrompting(title)
          }          
        }else{
          const page = {current:payload.page.current,pageSize:payload.page.pageSize}
          const faill = yield call(query, parse(page))
          if (faill.data.status == 200) {
            const detail = yield call(serial, faill.data.rows[0].id)
            if(detail.data.status == 200){
              yield put({type:'showDetail'})
              yield put({
                type: 'querySuccess',
                payload: {
                  list: faill.data.rows,
                  detail:detail.data.rows,
                  pagination: {total:faill.data.total,current:faill.data.offset + 1,pageSize:(faill.data.pageSize)},
                }
              })
            }
          }
        }
      }
    },
    *manufacture({payload},{call,put}){
      const data = yield call(MFTAut, payload)
      if(data.data.status == 200){
        if(payload.aut ==1){
          yield put({type:'showApproved'})
        }else{
          const page = {current:payload.page.current,pageSize:payload.page.pageSize}
          const faill = yield call(query, parse(page))
          if (faill.data.status == 200) {
            const detail = yield call(serial, faill.data.rows[0].id)
            if(detail.data.status == 200){
              yield put({type:'showDetail'})
              yield put({
                type: 'querySuccess',
                payload: {
                  list: faill.data.rows,
                  detail:detail.data.rows,
                  pagination: {total:faill.data.total,current:faill.data.offset + 1,pageSize:(faill.data.pageSize)},
                }
              })
            }
          }
        }
      }
    },
    *Approved({payload},{call,put}){
      console.log("payload===",payload)
        const audit = yield call(Audits, payload.data)
        if(audit){
          const page = {current:payload.page.current,pageSize:payload.page.pageSize,autStatus:payload.autStatus}
          const data = yield call(query, parse(page))
          if(data){
            yield put({type:'showDetail'})
            yield put({
              type: 'querySuccess',
              payload: {
                list: data.data.rows,
                pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
                autStatus:payload.autStatus
              }
            })
          }
        }
    },
    *showEdits({payload},{call,put}){
        const data = yield call(serial, payload.orderSerial)
        if(data){
            yield put({
                type: 'showEdit',
                payload: {
                    detail:data.data.rows
                }
            })
        }
    },
  },

  reducers: {
    querySuccess (state, action) {
      return { ...state,
        ... action.payload,
        pagination: {
          ...state.pagination,
          ...action.payload.pagination,
        } }
    },
    initialization(state, action){
      return {
        ...state,IndexListBon:true
      }
    },
    detailsit(state,action){
        return { ...state, ...action.payload,IndexListBon:false}
    },
    showDetail(state,action){
        return { ...state, DetailTags:true,ApprovedsTagBon:false,Editstags:false}
    },
    showApproved(state,action){
        return { ...state, DetailTags:false,ApprovedsTagBon:true,Editstags:false}
    },
    showEdit(state,action){
       return { ...state,...action.payload, DetailTags:false,ApprovedsTagBon:false,Editstags:true}
    }
  },

}
